<template>
  <el-dialog :visible.sync="dialogVisible" modal style="height: 850px" lock-scroll>
    <div slot="title">
      生成代码
    </div>

    <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="100px">
      <el-form-item label-width="120px" label="后端生成路径" prop="javaGeneratePath">
        <el-input v-model="formData.javaGeneratePath" placeholder="请输入后端生成路径"
                  :style="{width: '100%'}"></el-input>
      </el-form-item>
      <el-form-item label-width="120px" label="前端生成路径" prop="htmlGeneratePath">
        <el-input v-model="formData.htmlGeneratePath" placeholder="暂时没有用到"
                  :style="{width: '100%'}"></el-input>
      </el-form-item>
      <el-form-item label-width="120px" label="选择代码模板" prop="templateType">
        <el-select v-model="formData.templateType" placeholder="请选择代码模板" clearable :style="{width: '100%'}">
          <el-option v-for="(item, index) in templateTypeOptions" :key="index" :label="item.templateName"
                     :value="item.id" :disabled="item.disabled"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label-width="120px" label="生成包路径" prop="packagePath">
        <el-input v-model="formData.packagePath" placeholder="类的包路径" clearable :style="{width: '100%'}">
        </el-input>
      </el-form-item>
      <el-form-item label-width="120px" label="模块名" prop="moduleName">
        <el-input v-model="formData.moduleName" placeholder="用于url链接前缀" clearable :style="{width: '100%'}">
        </el-input>
      </el-form-item>
      <el-form-item label-width="120px" label="子模块名" prop="childModuleName">
        <el-input v-model="formData.childModuleName" placeholder="暂时没有用" clearable :style="{width: '100%'}">
        </el-input>
      </el-form-item>
      <el-form-item label-width="120px" label="生成功能描述" prop="desc">
        <el-input v-model="formData.desc" placeholder="请输入生成功能描述" clearable :style="{width: '100%'}">
        </el-input>
      </el-form-item>
      <el-form-item label-width="120px" label="生成功能作者" prop="author">
        <el-input v-model="formData.author" placeholder="请输入生成功能作者" clearable :style="{width: '100%'}">
        </el-input>
      </el-form-item>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button @click="handleGenerate()">生成</el-button>
    </div>
  </el-dialog>

</template>

<script>
  import linkApi from '@/api/database/link'
  import tableApi from '@/api/database/table'
  import templateApi from '@/api/gen/config/template'
  import genCodeApi from '@/api/gen/genCode'
  import {Notification} from 'element-ui';

  export default {
    name: "genDialog",
    data() {
      return {
        dialogVisible: false,
        tableData: {},
        formData: {
          databaseTableId:undefined,
          javaGeneratePath: undefined,
          htmlGeneratePath: undefined,
          templateType: undefined,
          packagePath: undefined,
          desc: undefined,
          author: undefined,
        },
        rules: {
          javaGeneratePath: [{
            required: true,
            message: '请输入后端生成路径',
            trigger: 'blur'
          }],
          htmlGeneratePath: [{
            required: true,
            message: '请输入前端生成路径',
            trigger: 'blur'
          }],
          templateType: [{
            required: true,
            message: '请选择代码模板',
            trigger: 'change'
          }],
          packagePath: [{
            required: true,
            message: '请输入生成包路径',
            trigger: 'blur'
          }],
          moduleName: [{
            required: true,
            message: '请输入模块名',
            trigger: 'blur'
          }],
          desc: [{
            required: true,
            message: '请输入生成功能描述',
            trigger: 'blur'
          }],
          author: [{
            required: true,
            message: '请输入生成功能作者',
            trigger: 'blur'
          }],
          childModuleName:[],
        },
        templateTypeOptions: [],
      }
    },
    created() {
      this.getAllTemplate();
    },
    methods: {
      getAllTemplate() {
        templateApi.findAll().then(res => {
          this.templateTypeOptions = res.data;
        })
      },

      handleGenerate() {
        genCodeApi.genCode(this.formData).then(res=>{
          console.log(res);
          if (res.success) {
            Notification({
              title: '成功',
              message: '删除数据成功!',
              type: 'success',
              duration: 2000
            });
            this.findAllTable();
          } else {
            Notification({
              title: '失败',
              message: '删除数据失败!',
              type: 'error',
              duration: 2000
            })
          }
        })
      },
      openDialog(data) {
        this.tableData =data;
        this.formData.databaseTableId = data.id;
        this.dialogVisible = true;
      }
    }
  }
</script>

<style scoped>

</style>
